package com.googlecode.jmicroerp.wicket.butoaie;


import com.googlecode.jmicroerp.wicket.DefaultViewPage;
import com.googlecode.jmicroerp.model.Recipient;
import com.googlecode.jmicroerp.service.ApometreService;
import com.googlecode.jmicroerp.service.BonService;
import com.googlecode.jmicroerp.service.GenericService;
import java.util.List;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.spring.injection.annot.SpringBean;
import java.util.Calendar;
import java.util.Date;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
import java.util.Locale;

@SuppressWarnings("serial")
public class ButoaieDetailsPage extends DefaultViewPage {
    
    @SpringBean(name = "apometreService")
    private ApometreService apometreService;
    @SpringBean(name = "genericService")
    private GenericService<Recipient> recipientService;
    @SpringBean(name = "bonService")
    private BonService bonService;
    
    private Date today = new Date(); // data curenta    
    private Date azi = addDays(today, 0); // data azi resetata la miezul noptii
    private Date ieri = addDays(today, -1); // data ieri cu data curenta - o zi

    public static Date addDays(Date date, int days) {
        GregorianCalendar cal = new GregorianCalendar();
        cal.setTime(date);
        cal.add(Calendar.DATE, days);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        //cal.set(Calendar.AM_PM, Calendar.AM);
        cal.set(Calendar.MILLISECOND, 0);
                 
        return cal.getTime();
    }

    private Locale locale = new Locale("ro");

    private class ButoaieDetails extends ListView<Object[]> {

        public ButoaieDetails(String id, List<Object[]> data) {
            super(id, data);
        }

        @Override
        protected void populateItem(ListItem<Object[]> li) {
            Object[] object = li.getModelObject();
            li.add(new Label("recipient", object[0].toString()));
            li.add(new Label("dataAdaugarii", DateFormat.getDateInstance(DateFormat.SHORT, locale).format(object[1])));
            li.add(new Label("index", object[2].toString()));
            
            //DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            
            //Date dataStart = today;
            Date dataStart = addDays((Date) object[1], 0);
            Date dataEnd = addDays((Date) object[1], 1);
            Date dataAnte = addDays((Date) object[1], -1);
            
            //Cantitate vanduta dupa idMagazin si idProdus
            BigDecimal cantVand = bonService.queryCantitateProduseByMagazinByDay((Long) object[4], (Long) object[5], dataStart, dataEnd); 
            li.add(new Label("vandut", cantVand.toString()));
            
            Object[] indexObjI = apometreService.queryByRecipientAll((Long) object[3], dataAnte);
            // li.add(new Label("indexDataI", indexObjI == null ? "necitit" : DateFormat.getDateInstance(DateFormat.SHORT).format(indexObjI[0])));
            // li.add(new Label("perioada", indexObjI == null ? "0.00" : indexObjI[1].toString()));

            BigDecimal diferentaIdx = BigDecimal.ZERO;
            BigDecimal diferentaVal = BigDecimal.ZERO;
            if (indexObjI == null)  {
            	diferentaIdx = BigDecimal.ZERO;
                diferentaVal = cantVand;
            } else {
                BigDecimal valI = new BigDecimal(indexObjI[1].toString());
                BigDecimal valA = new BigDecimal(object[2].toString());
                diferentaIdx = valA.subtract(valI);
                diferentaVal = diferentaIdx.subtract(cantVand);
            }
            li.add(new Label("lblDifIdx", diferentaIdx.toString()));
            li.add(new Label("lblDifVal", diferentaVal.toString()));
        
        }
    }

   
    public ButoaieDetailsPage(Long idRecipient) {
        
        Recipient recipient = recipientService.getById(Recipient.class, idRecipient);
        final Label lbRecipient = new Label("lbRecipient", recipient.getDenumire());
        add(lbRecipient);
        final ButoaieDetails butoaieView = new ButoaieDetails("butoaieView", apometreService.queryApometre(idRecipient, azi));
        add(butoaieView);
        
        final Link butoaieLink = new Link("butoaieLink") {
            @Override
            public void onClick() {
            	setResponsePage(ButoaiePage.class);
            }
        };
        add(butoaieLink);
   
    }
}